package node_game

import (
	"sanguo/cluster"
	ss_msg "sanguo/protocol/ss/message"
	"sync"
	"time"
	"github.com/golang/protobuf/proto"
)

var report_once sync.Once
var selfService cluster.Service

//存放此game服的在線人數
var gamePlayerNum int32

func ReportInit(selfService_ cluster.Service, externalAddr string) {

	report_once.Do(func() {

		selfService = selfService_

		//启动一个go程，每10秒上报自己的服务状态
		go func() {
			report := &ss_msg.ReportGame{
				PeerID:       proto.String(selfService_.ToPeerID().ToString()),
				ExternalAddr: proto.String(externalAddr),
			}
			for {
				report.PlayerNum = proto.Int32(gamePlayerNum)
				//Infoln("report", report)
				cluster.Brocast("login", report)
				time.Sleep(time.Second * 10)
			}
		}()
	})
}

func UpdateGamePlayerNum(playerNum int32) {
	gamePlayerNum = playerNum
}

func GetSelfService() cluster.Service {
	return selfService
}
